home *** CD-ROM | disk | FTP | other *** search
- Path: news.gate.net!rdl-ilps-rxh2
- From: rmcinnis@gate.net (Robert B McInnis)
- Newsgroups: comp.lang.c++
- Subject: Re: File I/O using fwrite()
- Date: 18 Apr 1996 13:27:14 GMT
- Organization: 9th Bit Software
- Message-ID: <4l5fvi$295a@news.gate.net>
- References: <31758D70.1D38@vixa.voyager.net> <4l4s79$r1v@nadine.teleport.com>
- NNTP-Posting-Host: miafl2-50.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- >David Wade <dwade@vixa.voyager.net> wrote:
- >HELP!!! I am having a major problem getting random access file routines
- >to work properly. For some reason, no matter what I do the information
- >is always written at the end of the file instead of where I want it to
- >replace... All I need to do is update a record in a potentailly large
- >file, and I can't load the entire file and re-write it to do it. If
- >anyone sees what I am doing wrong, or has a possible solution using
- >other means, I would greatly appreciate the info. The funtion looks
- >like this:
- > f=_fsopen(".\\data\\weapons.dat", "ab+", SH_DENYNONE);
- > if(f == NULL)
- > {
- >[snip]
- > };
- > rewind(f);
- > fseek(f,(item_data.reference-1)*sizeof(item_struct),SEEK_SET);
- > fwrite(&item_data, sizeof(item_struct), 1, f);
- > fclose(f);
- >
- >
- >The struct is of type item_struct, and item_data is an instance of that
- >struct.
- >
-
- Try taking the append command out of the mode string. For random access
- binary files, use this mode string : "w+b" (order doesn't matter)
-
- Check for file existence and create it if you have to.
-
- Don't bother doing rewind. Just fseek with a SEEK_SET and a long offset.
-
- If you'd like a SMALL set of classes that does most of this for you, just ask.
- I also have a descendent class that works for Ms Windows specifically,
- otherwise it's just C++.
-
- Hope this helps,
-
- Rob
-
- ----------------------------
- Robert McInnis
- 9th Bit Software
- (eMail) rmcinnis@gate.net
- (web ) http://www.gate.net/~rmcinnis
-
-
-
-
-